#include "main.h"

FILE ** openArc (char * arcName, char * arcnew)
{
	int rc = 0;
	char a[] = ".awc";
	char buf[]=_SIGN;
	FILE ** arc = (FILE **) calloc (2, sizeof (FILE *));
	struct stat * arcstat = (struct stat *) calloc (1,sizeof(struct stat));
	envInf.fileStat = arcstat;
	if (!arc)	cl_exit (OPEN_ARC,MEMORY_ERR);
	if (!arcstat)	cl_exit (OPEN_ARC,MEMORY_ERR);
	arcnew = mktemp (arcnew);
	if ((arc[0] = fopen (arcName,"rb")))	{	
		envInf.old_arc = arc[0];
		if (!(rc = checkSign(arc[0])))	{	
			stat (arcName,arcstat);
			if (!(arcstat->st_mode & _S_IWRITE))	{	cl_exit (OPEN_ARC,ARC_IWRITE);	}
			if ((arc[1] = fopen (arcnew,"wb")))	{
				envInf.new_arc = arc[1];
				return arc;
			}
			else {	cl_exit (OPEN_ARC,OPEN_ERR);	}
		} else {	cl_exit (OPEN_ARC,SIGN_ERR);	}
	} else {	
		arc[0] = NULL;
		if ((arc[1] = fopen (arcnew,"wb")))	{
			envInf.new_arc = arc[1];
			return arc;	
		}
		else {	cl_exit (OPEN_ARC,OPEN_ERR);	}
	}
return NULL;
}
